const config = require("../config/index");
const jwt = require("jsonwebtoken");
const { Admin } = require("../models");

module.exports = async (req, res, next) => {
  const assert = require("http-assert");
  const token = req.headers.token;
  assert(token, 401, "请先登录");

  const { id } = await jwt.verify(token, config.jwt_secret);
  assert(id, 401, "请先登录");

  req.user = await Admin.findByPk(id);
  return next();
};
